package com.lanou.controller;

import com.lanou.entity.VipInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author wangxinleibobo
 */
@Controller
@RequestMapping(value = "/message")
public class MessageController {


    RestTemplate restTemplate=new RestTemplate();

    //点击我的惠买,根据id信息,进行数据的回显
    @RequestMapping(value = "/findMessage.do")
    @ResponseBody
    public ModelAndView findMessage(@RequestParam Map<String,Object> map) {
        System.out.println("接收到的id信息----"+map);

        //创建视图
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("view/message");
        //调用接口,将id传过去,得到vipInfo对象
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        System.out.println("传过去的数据--"+multiValueMap);
        VipInfo vipInfo = restTemplate.postForObject("http://10.90.87.218:8080/port/getvipInfoById.do",multiValueMap,VipInfo.class);
        System.out.println("点击惠买后信息的回显--"+vipInfo);
        //将回显数据放入域中
        modelAndView.addObject("result",vipInfo);
        return modelAndView;
    }


    /**
     *  银行卡信息的回显
     * @return
     */

    @RequestMapping("/messageBank.do")
    public ModelAndView toMessageBank(@RequestParam Map<String,Object> map){
        System.out.println("前端接收的id信息----"+map);
        //调用接口根据会员id查询银行卡信息,list集合

        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);

        //根据id查询用户名信息
        VipInfo vipInfo= restTemplate.postForObject("http://10.90.87.218:8080/bank/getVipInfoAccountByVipId.do", multiValueMap, VipInfo.class);
        System.out.println("获取会员名------"+vipInfo);
        //查询银行卡信息
        List list = restTemplate.postForObject("http://10.90.87.218:8080/bank/getBankInfoByVipId.do", multiValueMap, List.class);
        System.out.println("返回的银行卡集合信息-----"+list);

        //
        ModelAndView modelAndView=new ModelAndView("view/messageBank");
        modelAndView.addObject("result",list);
        System.out.println("获取到的银行卡信息--------"+list);
        modelAndView.addObject("vipInfo",vipInfo);
        return modelAndView;
    }

    /**
     *  提交表单数据之后更新会员信息
     * @return
     */
    @RequestMapping("/updateVipInfo.do")
    @ResponseBody
    public Map<String, Object> updateVipInfo(@RequestParam Map<String,Object> map){
        System.out.println("接收到的form表单数据-----"+map);

        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);

        //调用接口根据id进行会员数据的更新
        Map map1 = restTemplate.postForObject("http://10.90.87.218:8080/vip/updateByIdSelective.do", (Object) multiValueMap,Map.class);
        if(map1.get("result").equals("success")){
            return map1;
        }else {
            return map1;
        }

    }

    /**
     *  添加收获地址获取form表单进行数据的添加
     * @return
     */

    @RequestMapping("/messageAddress.do")
    @ResponseBody
    public Map messageAddress(@RequestParam Map<String,Object> map){
        System.out.println("获取form表单数据-------"+map);

        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);


        //调用接口,根据会员编号向addrInfo地址表插入数据  addAddrInfoByVipNo
        Map map1 = restTemplate.postForObject("http://10.90.87.218:8080/addr/addAddrInfoByVipNo.do",multiValueMap,Map.class);
        System.out.println("返回的结果-----"+map1);
        return map1;
    }

    /**
     *  会员添加银行卡
     * @return  addBankInfo
     */

    @RequestMapping("/addBank.do")
    @ResponseBody
    public Map addBank(@RequestParam Map<String,Object> map){
        System.out.println("接收的form表单添加银行卡信息---"+map);

        //调用添加银行卡信息的接口进行会员银行卡的添加
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        Map map1 = restTemplate.postForObject("http://10.90.87.218:8080/bank/addBankInfo.do",multiValueMap,Map.class);
        return map1;
    }
}

